package com.yao.handler;

import com.alibaba.fastjson.JSONObject;
import com.yao.bean.R;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.common.exceptions.BadClientCredentialsException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Copyright (C), 2022-2022, 姚兵
 * Author: 32210
 * Date: 2022/10/10 13:41
 * FileName: MyAuthenticationEntryPoint
 * Description: 123
 */
@Component
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        response.setStatus(200);
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();


        R r = new R();
        r.setCode(455);
        if(authException instanceof UsernameNotFoundException){
            r.setData("");
            r.setMsg(authException.getMessage());
            out.write(JSONObject.toJSONString(r));
            out.flush();
            out.close();
            return;
        }

        if(authException instanceof  InsufficientAuthenticationException){
            InsufficientAuthenticationException ae= (InsufficientAuthenticationException) authException;
            System.out.println("authException:"+authException);
            r.setData(authException);
            Throwable cause = authException.getCause();

            OAuth2Exception e=null;
            if(cause instanceof OAuth2Exception) e = (OAuth2Exception) cause;
            if(e==null)return;
            r.setMsg(e.getMessage());
            r.setData(e.getOAuth2ErrorCode());
            out.write(JSONObject.toJSONString(r));
            out.flush();
            out.close();
            return;
        }


        if(authException instanceof BadCredentialsException){

            BadCredentialsException exception= (BadCredentialsException) authException;
            BadClientCredentialsException credentialsException= (BadClientCredentialsException) exception.getCause();
            r.setMsg("用户名密码错误");
            if(credentialsException!=null){
                r.setData(credentialsException.getOAuth2ErrorCode());
            }

            out.write(JSONObject.toJSONString(r));
            out.flush();
            out.close();
//            throw authException;
        }




    }
}
